<template>
  <div class="app-container">
    <el-form
  :model="queryParams"
  ref="queryRef"
  :inline="true"
  class="mb-4"
>
  <!-- 用户名称 -->
  <el-form-item label="用户名称" prop="userName">
    <el-input
      v-model="queryParams.userName"
      placeholder="请输入用户名称"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 手机号 -->
  <el-form-item label="手机号" prop="phone">
    <el-input
      v-model="queryParams.phone"
      placeholder="请输入手机号"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 小程序 openId -->
  <el-form-item label="OpenID" prop="openId">
    <el-input
      v-model="queryParams.openId"
      placeholder="请输入 OpenID"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 用户ID -->
  <el-form-item label="用户ID" prop="id">
    <el-input
      v-model="queryParams.id"
      placeholder="请输入用户ID"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 标签 -->
  <el-form-item label="标签" prop="label">
    <el-input
      v-model="queryParams.label"
      placeholder="请输入标签"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 注释 -->
  <el-form-item label="注释" prop="note">
    <el-input
      v-model="queryParams.note"
      placeholder="请输入注释"
      clearable
      style="width: 200px"
    />
  </el-form-item>

  <!-- 按钮区域 -->
  <el-form-item>
    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  </el-form-item>
</el-form>



    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange" border
    >
      <el-table-column label="用户ID" prop="id" width="175" />
      <el-table-column label="openID" prop="openId" width="263" />
      <el-table-column label="小鹅通用户ID" prop="xiaoetongUserId" width="200" />
      <el-table-column label="用户名称" prop="userName" width="200" />
      <el-table-column label="头像" prop="avatar" width="200" />
      <el-table-column label="性别" prop="sex" width="200" >  <template #default="scope">
    <el-tag type="success" v-if="scope.row.sex === '0'">
      男
    </el-tag>
    <el-tag type="warning" v-else-if="scope.row.sex === '1'">
      女
    </el-tag>
  </template></el-table-column>
      <el-table-column label="邮箱地址" prop="email" width="200" />
      <el-table-column label="邀请码" prop="inviteCode" width="200" />
      <el-table-column label="标签" prop="label" width="200" />
      <el-table-column label="注释" prop="note" width="200" />
      <el-table-column label="最后登录IP" prop="lastIp" width="200" />
      <el-table-column
        label="角色名称"
        prop="name"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="权限字符"
        prop="roleKey"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="显示顺序" prop="sort" width="100" />

      <el-table-column label="最后登录时间" align="center" prop="lastTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="修改时间" align="center" prop="updateTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />

    

  </div>
</template>

<script setup name="User">
import { listRole } from "@/api/min-app/index";
const { proxy } = getCurrentInstance();

const roleList = ref([]);   // 其实这里应该叫 userList
const loading = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    page: 1,
    size: 10,
    userName: "",   // 用户名称
    phone: "",      // 手机号
    openId: "",     // 小程序用户标识
    label: "",      // 标签
    note: "",       // 注释
    id: undefined,  // 用户ID（可选）
  },
});

const { queryParams } = toRefs(data);

/** 查询用户列表 */
function getList() {
  loading.value = true;

  const params = {
    id: queryParams.value.id,
    label: queryParams.value.label,
    note: queryParams.value.note,
    openId: queryParams.value.openId,
    page: queryParams.value.page,
    phone: queryParams.value.phone,
    size: queryParams.value.size,
    userName: queryParams.value.userName,
  };

  listRole(params).then(res => {
    roleList.value = res.data.records;
    total.value = Number(res.data.total);
    loading.value = false;
  });
}

/** 搜索按钮 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.page = 1;
  getList();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  // 此接口没有 roleId，这里不再处理 ids
}

getList();
</script>
